#include "digi_tube.hpp"
int digi_tube::digi_number_interger = 8524;
float digi_tube::digi_number_float = 0;
bool digi_tube::is_number_float = 0.0;
bool digi_tube::is_number_negative = 0;
int digi_tube::loops_num_counter = 0;
char sigle_digi_number = 0;
char digi_tube::led_num_to_display[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
void digi_tube::digi_tube_init(void)
{
    Timer_setup::Timer_init(100, 360, TIM2_BASE, TIM2_IRQn, 2, 3);
    Timer_setup::add_callback_function(digi_tube::number_display_scan);
}
void digi_tube::number_display_scan()
{
    uint16_t temp_gpio_status = GPIO_ReadInputData(GPIOB), temp_display_numbers = 0;
    loops_num_counter++;
    temp_gpio_status &= 0x00ff;
    GPIO_WriteBit(GPIOB, GPIO_Pin_3, Bit_RESET);
    GPIO_Write(GPIOB, temp_gpio_status);
    switch (loops_num_counter)
    {
    case 1:
        GPIO_WriteBit(GPIOB, GPIO_Pin_4, Bit_RESET);
        GPIO_WriteBit(GPIOB, GPIO_Pin_5, Bit_RESET);
        sigle_digi_number = digi_number_interger % 10;
        break;
    case 2:
        GPIO_WriteBit(GPIOB, GPIO_Pin_4, Bit_SET);
        GPIO_WriteBit(GPIOB, GPIO_Pin_5, Bit_RESET);
        sigle_digi_number = digi_number_interger % 100;
        sigle_digi_number /= 10;
        break;
    case 3:
        GPIO_WriteBit(GPIOB, GPIO_Pin_4, Bit_RESET);
        GPIO_WriteBit(GPIOB, GPIO_Pin_5, Bit_SET);
        sigle_digi_number = digi_number_interger / 100;
        sigle_digi_number %= 10;
        break;
    case 4:
        GPIO_WriteBit(GPIOB, GPIO_Pin_4, Bit_SET);
        GPIO_WriteBit(GPIOB, GPIO_Pin_5, Bit_SET);
        sigle_digi_number = digi_number_interger / 1000;
        sigle_digi_number %= 10;
        loops_num_counter = 0;
        break;
    default:
        loops_num_counter = 0;
        break;
    }

    temp_display_numbers = led_num_to_display[sigle_digi_number];
    if (loops_num_counter == 2 && is_number_float)
    {
        temp_display_numbers |= 0x80; //加小数点
    }
    if (loops_num_counter == 0 && is_number_negative)
    {
        temp_display_numbers = 0x40; //加负号
    }
    // GPIO_WriteBit(GPIOB, GPIO_Pin_4, Bit_RESET);
    temp_gpio_status = GPIO_ReadInputData(GPIOB);
    temp_display_numbers <<= 8;
    temp_gpio_status &= 0x00ff;
    temp_gpio_status |= temp_display_numbers;
    GPIO_Write(GPIOB, temp_gpio_status);
    GPIO_WriteBit(GPIOB, GPIO_Pin_3, Bit_SET);
    // temp_gpio_status &= 0x00ff;
    // GPIO_Write(GPIOB, temp_gpio_status);
}
void digi_tube::set_number(float number)
{
    if (number < 0)
    {
        is_number_negative = 1;
    }
    digi_number_float = number * 10;
    is_number_float = 1;
    digi_number_interger = digi_number_float;
}
void digi_tube::set_number(int number)
{
    if (number < 0)
    {
        is_number_negative = 1;
    }
    digi_number_interger = number;
}